﻿using System;
using Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.VisualBasic;
using m_pricelist.Proposal_Services;
using System.Windows;

namespace m_pricelist.Services
{
    class ProjectProposal //: Proposal
    {
        /*
        ProposalService ps = new ProposalService();


        string m_sCurrency { get; set; }
        string m_sFinaldest { get; set; }
        string m_sFirstPort { get; set; }

        #region ProjectProposal
        public void GetProjectProposal(object sPath, object sFileToOpen, Excel.Worksheet xlSheet, Word.Application wdApp,
            Excel.Workbooks xlWorkBookObjects, Excel.Workbook xlWorkBook, Excel.Application xlApp) 
        {
            //Word.Application wdApp = null;
            Word.Document wdDoc = null;

            Word.Document wdDocTerms = null;

            Word.Table tbl = null;
            Word.Range PRange = null;
            Word.Range rTerms = null;

            string specifier = "###,##0";
            string prosenta = "0.0%";

            int lRow = 0;

            int lwtRow = 0;
            int lPSRow = 0;

            string sCurrancy = null;


            double dDiscount = 0;

            wdApp.ScreenUpdating = false;

            wdDocTerms = wdApp.Documents.Open(sPath + "PaymentTerms.docx");
            rTerms = wdDocTerms.Bookmarks.get_Item("LC").Range;

            lPSRow = ps.GetLastUsedCell(1, "PriceSummary", xlSheet, xlWorkBook, wdApp) - 3;
            sCurrancy = Convert.ToString(xlApp.Sheets["Main Overview"].Range("_Currency").Value);
            wdDoc = wdApp.Documents.Open(sPath + "Marel Proposal Template.docx");

            PRange = wdDoc.Bookmarks.get_Item("CuName").Range;
            PRange.Text = Convert.ToString(xlApp.Sheets["Main Overview"].Range("_AccName").Value);
            wdDoc.Bookmarks.Add("CuName", PRange);

            PRange = wdDoc.Bookmarks.get_Item("CuAddress").Range;
            PRange.Text = Convert.ToString(xlApp.Sheets["Main Overview"].Range("_BillingAddress").Value);
            wdDoc.Bookmarks.Add("CuAddress", PRange);

            PRange = wdDoc.Bookmarks.get_Item("Date1").Range;
            PRange.Text = Convert.ToString(xlApp.Sheets["Main Overview"].Range("_Date").Value);
            wdDoc.Bookmarks.Add("Date1", PRange);

            PRange = wdDoc.Bookmarks.get_Item("Layout1").Range;
            PRange.Text = Convert.ToString(xlApp.Sheets["Main Overview"].Range("_Layout").Value);
            wdDoc.Bookmarks.Add("Layout1", PRange);

            PRange = wdDoc.Bookmarks.get_Item("CuName2").Range;
            PRange.Text = Convert.ToString(xlApp.Sheets["Main Overview"].Range("_AccName").Value);
            wdDoc.Bookmarks.Add("CuName2", PRange);

            PRange = wdDoc.Bookmarks.get_Item("Price2").Range;
            PRange.Text = "EUR 289.379 , CIF " + Convert.ToString(xlApp.Sheets["Main Overview"].Range("_ShippingAddress").Value);
            wdDoc.Bookmarks.Add("CuShipping", PRange);

            PRange = wdDoc.Bookmarks.get_Item("Payment").Range;
            PRange.Text = rTerms.Text;
            wdDoc.Bookmarks.Add("Payment", PRange);
            wdDocTerms.Close(false);
            rTerms = null;

            PRange = wdDoc.Bookmarks.get_Item("Price").Range;

            foreach (Word.Table oTable in wdDoc.Tables)
            {
                if ((Strings.InStr(1, oTable.Cell(1, 1).Range.Text, "Area", CompareMethod.Binary) != 0) & (Strings.InStr(1, oTable.Cell(1, 2).Range.Text, "Description", CompareMethod.Text) != 0))
                {
                    int j = oTable.Rows.Count;

                    for (int l = j; l >= 1; l--)
                    {
                        oTable.Rows[l].Delete();
                    }

                    break;
                }
            }

            dDiscount = Convert.ToDouble(xlApp.Sheets["PriceSummary"].Cells(lPSRow + 3, 4).Value);

            if (dDiscount > 0)
            {
                tbl = wdDoc.Tables.Add(PRange, lPSRow, 5);
                tbl.Rows.Height = 1;
                tbl.Columns[1].Width = 50;
                tbl.Columns[2].Width = 230;
                tbl.Columns[3].Width = 60;
                tbl.Columns[4].Width = 60;
                tbl.Columns[5].Width = 70;


                tbl.Rows[1].Range.Font.Bold = 1;
                tbl.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                tbl.Cell(1, 1).Range.Text = "Area";
                tbl.Cell(1, 2).Range.Text = "Description";
                tbl.Cell(1, 3).Range.Text = "Price(" + sCurrancy + ")";
                tbl.Cell(1, 4).Range.Text = "Discount";
                tbl.Cell(1, 5).Range.Text = "Contract Price(" + sCurrancy + ")";
                tbl.Cell(1, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                tbl.Rows[1].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
            }
            else
            {
                wdDoc.Tables.Add(PRange, lPSRow, 3);
                tbl.Rows.Height = 1;
                tbl.Columns[1].Width = 60;
                tbl.Columns[2].Width = 300;
                tbl.Columns[3].Width = 90;


                tbl.Rows[1].Range.Font.Bold = 1;
                tbl.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                tbl.Cell(1, 1).Range.Text = "Area";
                tbl.Cell(1, 2).Range.Text = "Description";
                tbl.Cell(1, 3).Range.Text = "Contract Price(" + sCurrancy + ")";
                tbl.Cell(1, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                tbl.Rows[1].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
            }

            lwtRow = 2;

            for (int iRow = 2; iRow <= lPSRow; iRow++)
            {

                if (Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value).IndexOf("Total") + 1 != 0)
                {

                    if (Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3 - 1, 3).Value) == "")
                    {
                        tbl.Rows[lwtRow - 1].Delete();
                        lwtRow = lwtRow - 1;
                    }

                    tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value);
                    tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value).ToString(specifier));
                    tbl.Cell(lwtRow, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

                    if (dDiscount > 0)
                    {
                        tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 4).Value).ToString(prosenta));
                        tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value).ToString(specifier));
                        tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                        tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                    }

                    tbl.Rows[lwtRow].Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    tbl.Rows[lwtRow].Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
                    tbl.Rows[lwtRow].Range.Font.Bold = 1;
                }
                else
                {
                    string str = Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value);
                    switch (str)
                    {
                        case "0":

                            tbl.Cell(lwtRow, 1).Range.Text = Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value);
                            tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 2).Value);
                            tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value).ToString(specifier));
                            tbl.Cell(lwtRow, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                            if (dDiscount > 0)
                            {
                                tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 4).Text);
                                tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value).ToString(specifier));
                                tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                            }

                            break;
                        default:

                            tbl.Cell(lwtRow, 1).Range.Text = Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 1).Value);
                            tbl.Cell(lwtRow, 2).Range.Text = Convert.ToString(xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 2).Value);
                            tbl.Cell(lwtRow, 3).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 3).Value).ToString(specifier));
                            tbl.Cell(lwtRow, 3).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                            if (dDiscount > 0)
                            {
                                tbl.Cell(lwtRow, 4).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 4).Value).ToString(prosenta));
                                tbl.Cell(lwtRow, 5).Range.Text = Convert.ToString((xlApp.Sheets["PriceSummary"].Cells(lRow + 3, 5).Value).ToString(specifier));
                                tbl.Cell(lwtRow, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                tbl.Cell(lwtRow, 5).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                            }
                            break;
                    }

                }

                lwtRow = lwtRow + 1;
            }

            wdApp.ScreenUpdating = true;
            wdApp.Visible = true;
        }
        #endregion

        #region UserForm_Initialize_Project
        public void UserForm_Initialize_Project(Excel.Worksheet xlSheet, Word.Application wdApp,
            Excel.Workbooks xlWorkBookObjects, Excel.Workbook xlWorkBook, Excel.Application xlApp, ref bool b_project)
        {
            string m_sPath = " ";
            object customPropertiesObject = null;
            string sDelivery = null;
            Excel.Range rFundin = null;

            try
            {
                customPropertiesObject = xlWorkBook.CustomDocumentProperties;
                xlSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("Registration");

                m_sCurrency = xlWorkBook.Worksheets["Main Overview"].Range("_Currency").Value;
                m_sFinaldest = xlWorkBook.CustomDocumentProperties("FinalDest").Value;
                sDelivery = xlWorkBook.CustomDocumentProperties("DeliveryTerms").Value;

                m_sFirstPort = xlWorkBook.CustomDocumentProperties("FirstPort").Value;

                //Call chkSeaOnly_Change
                chkSeaOnly_CheckedChanged(this, EventArgs.Empty);
                //    If sSolutionType = "C" Then
                chkApp3.Checked = true;
                chkApp3.Enabled = false;

                //If og else setningar sem tengjast Delivery terms Frame
                if (sDelivery == "FOB")
                {
                    optFob.Checked = true;
                    txtFinalAdress.Top = 36;
                    chkSeaOnly.Checked = true;
                    txtFinalAdress.Text = m_sFirstPort;
                }
                //fullt af If og else setningum
                if (sDelivery == "FOB")
                {
                    optFob.Checked = true;
                    txtFinalAdress.Top = 36;
                    chkSeaOnly.Checked = true;
                    txtFinalAdress.Text = m_sFirstPort;
                }
                else if (sDelivery == "CFR")
                {
                    optCFR.Checked = true;
                    txtFinalAdress.Top = 58;
                    chkSeaOnly.Checked = true;
                    txtFinalAdress.Text = (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                }
                else if (sDelivery == "CIF")
                {
                    optCIF.Checked = true;
                    txtFinalAdress.Top = 81;
                    chkSeaOnly.Checked = true;
                    txtFinalAdress.Text = (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
                }
                else if (sDelivery == "FCA")
                {
                    optFCA.Checked = true;
                    txtFinalAdress.Top = 36;
                    txtFinalAdress.Text = m_sFirstPort;
                }
                else if (sDelivery == "CPT")
                {
                    optCPT.Checked = true;
                    txtFinalAdress.Top = 58;
                    txtFinalAdress.Text = m_sFinaldest;
                }
                else if (sDelivery == "CIP")
                {
                    optCIP.Checked = true;
                    txtFinalAdress.Top = 81;
                    txtFinalAdress.Text = m_sFinaldest;
                }
                else if (sDelivery == "DAP")
                {
                    optDAP.Checked = true;
                    txtFinalAdress.Top = 102;
                    txtFinalAdress.Text = m_sFinaldest;
                }
                else if (sDelivery == "DAT")
                {
                    optDAT.Checked = true;
                    txtFinalAdress.Top = 122;
                    txtFinalAdress.Text = m_sFinaldest;
                }

                object What = "inn";
                object After = xlSheet.Cells[5000, 5];
                object LookIn = Excel.XlFindLookIn.xlValues;
                object LookAt = Excel.XlLookAt.xlPart;
                object SearchOrder = Excel.XlSearchOrder.xlByRows;
                Excel.XlSearchDirection SearchDirection = Excel.XlSearchDirection.xlNext;
                object MatchCase = System.Reflection.Missing.Value;
                object MatchByte = System.Reflection.Missing.Value;
                object SearchFormat = System.Reflection.Missing.Value;

                rFundin = xlSheet.Cells.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, Type.Missing, Type.Missing, Type.Missing);
                if (rFundin == null)
                    m_bInnova = false;
                else
                    m_bInnova = true;

                m_sPath = Interaction.GetSetting(AppName: "Marel_Consulting", Section: "MFS_Pricelist", Key: "Pricelist_Location");

                ps.InstallParameter(m_tInstalParam, ref b_pricelist, ref b_project, xlWorkBook, xlApp);

                if (m_tInstalParam.bIHardware)
                { chkHardware.Checked = true; m_bInstallation = true; }
                else
                    chkHardware.Checked = false;
                if (m_tInstalParam.bTHardware) { chkTHardware.Checked = true; m_bInstallation = true; }
                else
                    chkTHardware.Checked = false;
                if (m_tInstalParam.bISoftware) { chkSoftware.Checked = true; m_bInstallation = true; }
                else
                    chkSoftware.Checked = false;
                if (m_tInstalParam.bTSoftware) { chkTSoftware.Checked = true; m_bInstallation = true; }
                else
                    chkTSoftware.Checked = false;

                //Radio buttons eru ekki með value, hlýtur að eiga vera checked
                //optOneLang.Value = True og optStandard.Value = True
                optOneLang.Checked = true;
                optStandard.Checked = true;
            }
            catch (Exception ex)
            {
                ps.CleanUp(string.Empty, wdApp);
                MessageBox.Show(ex.ToString());
            }

        }
        #endregion
        */
    }
}
